package com.wangge.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DymanicSubject implements InvocationHandler {
  private Object sub;
  
  public DymanicSubject(Object sub) {
    this.sub = sub;
  }
  
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("--代理调用前");
    method.invoke(sub, args);
    System.out.println("--代理调用后");
    return null;
  }
  
  public static void main(String[] args) {
    Subject sub = new DefaultSubject();
    DymanicSubject ds = new DymanicSubject(sub);
    Class<? extends Subject> cls = sub.getClass();
    Subject subject = (Subject) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), ds);
    subject.request();
  }
}
